using UnityEngine;
using Unity.VisualScripting;
using GameFramework.Sound;
using UnityGameFramework.Runtime;

namespace LS.UGFUnits
{
    [UnitShortTitle("Play Sound")]
    public class PlaySoundInPosition: SoundUnit
    {
        [DoNotSerialize]
        public ValueInput Asset { get; set; }
        
        [DoNotSerialize]
        public ValueInput Group { get; set; }
        
        [DoNotSerialize]
        public ValueInput Priority { get; set; }
        
        [DoNotSerialize]
        public ValueInput Position { get; set; }
        
        [DoNotSerialize]
        public ValueInput PlaySoundParams { get; set; }
        
        [DoNotSerialize]
        [PortLabelHidden]
        public ValueOutput SerialId { get; set; }

        protected override void Definition()
        {
            base.Definition();
            Asset = ValueInput<string>(nameof(Asset), string.Empty);
            Group = ValueInput<string>(nameof(Group), string.Empty);
            Priority = ValueInput<int>(nameof(Priority), 0);
            Position = ValueInput<Vector3>(nameof(Position), Vector3.zero);
            PlaySoundParams = ValueInput<PlaySoundParams>(nameof(PlaySoundParams), null);
            SerialId = ValueOutput<int>(nameof(SerialId));
        }

        protected override ControlOutput In(Flow flow)
        {
            string asset = flow.GetValue<string>(Asset);
            string group = flow.GetValue<string>(Group);
            int priority = flow.GetValue<int>(Priority);
            Vector3 position = flow.GetValue<Vector3>(Position);
            PlaySoundParams playSoundParams  = flow.GetValue<PlaySoundParams>(PlaySoundParams);
            int serialId = Target?.PlaySound(asset, group, priority, playSoundParams, position) ?? 0;
            flow.SetValue(SerialId, serialId);
            return base.In(flow);
        }
    }
}